package com.deschen.security.core.social.qq.connect;

import com.deschen.security.core.social.qq.api.QQ;
import com.deschen.security.core.social.qq.api.QQImpl;
import org.springframework.social.oauth2.AbstractOAuth2ServiceProvider;
import org.springframework.social.oauth2.OAuth2Operations;
import org.springframework.social.oauth2.OAuth2Template;

/**
 * @Author deschen
 * @Create 2019/9/1
 * @Description spring social OAuth2.0 流程代码
 * @Since 1.0.0
 */
// 文档：https://wiki.connect.qq.com/使用authorization_code获取access_token
public class QQServiceProvider extends AbstractOAuth2ServiceProvider<QQ> {

    private String appId;

    // 将用户导向认证服务器获取授权码链接
    private static final String AUTHORIZE_URL = "https://graph.qq.com/oauth2.0/authorize";
    // 获取认证服务器授权码申请令牌链接
    private static final String ACCESSTOKEN_URL = "https://graph.qq.com/oauth2.0/token";

    /**
     * OAuth2.0 第一到第五流程
     * 系统级配置，说明是哪个应用
     * @param appId
     * @param appSecret
     */
    public QQServiceProvider(String appId, String appSecret) {
        super(new QQOAuth2Template(appId, appSecret, AUTHORIZE_URL, ACCESSTOKEN_URL));
        this.appId = appId;
    }

    /**
     * 获取qq用户信息
     * @param accessToken
     * @return
     */
    @Override
    public QQ getApi(String accessToken) {
         return new QQImpl(accessToken, appId);
    }
}
